package leetcode.leetcode876;

/**
 * @author czt
 * @version 1.0
 * @since 2024/12/18
 *
 * 查找链表中间节点
 */
public class Leetcode876 {

    public ListNode middleNode(ListNode head) {
        /*
                p1
                        p2
        1   2   3   4   5   null
    --------------------------------------
                    p1
                                p2
        1   2   3   4   5   6   null
         */
        // 快慢指针法
        ListNode p1 = head;
        ListNode p2 = head;
        while (p2 != null && p2.next != null) {
            // p1走一步，p2走两步
            p1 = p1.next;
            p2 = p2.next.next;
        }

        return p1;
    }
}
